package com.abc.controller;

import com.abc.entity.*;
import com.abc.service.IUserService;
import com.abc.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(path = "/users", produces = "application/json;charset=utf-8")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @RequestMapping("/reg")
    public JsonResult<Void> reg(UserRegDto userRegDto) {
        // 执行注册
        userService.reg(userRegDto);
        // 返回成功
        JsonResult<Void> jr = new JsonResult<Void>();
        jr.setState(1);
        return jr;
    }
    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public JsonResult<Void> login(UserLoginDTO loginDTO, HttpServletRequest request) {
        userService.login(loginDTO);
        User user = userService.selectOneUser(loginDTO.getUsername());
        request.getSession().setAttribute("user", user);
        return new JsonResult(1);
    }
    @RequestMapping("/listWorkers")
    public JsonResult<Void> list(String name){
        List<Workers> list = userService.list(name);
        return new JsonResult(1,list);
    }
    @RequestMapping("/deleteWorkers")
    public JsonResult<Void> deleteWorkers(String id) {
        userService.deleteWorkers(id);
        return new JsonResult(1,"删除成功");
    }
    @RequestMapping("/deleteRuzhi")
    public JsonResult<Void> deleteRuzhi(String id) {
        userService.deleteRuzhi(id);
        return new JsonResult(1,"删除成功");
    }
    @RequestMapping("/addJQWorkers")
    public JsonResult<Void> addJQWorkers() {
        return new JsonResult(1);
    }
    @RequestMapping("/addJQRuzhi")
    public JsonResult<Void> addJQRuzhi() {
        return new JsonResult(1);
    }
    @RequestMapping("/updJQWorkers")
    public JsonResult<Void> updJQWorkers() {
        return new JsonResult(1);
    }
    @RequestMapping("/selectInfo")
    public JsonResult<Void> selectInfo(String id){
        Workers workers = userService.listById(id);
        return new JsonResult(1,workers);
    }
    @RequestMapping("/updateWorkers")
    public JsonResult<Void> updateWorkers(Workers workers){
        userService.updateWorkers(workers);
        return new JsonResult(1);
    }
    @RequestMapping("/addWorkers")
    public JsonResult<Void> addWorkers(Workers workers){
        userService.insertWorkers(workers);
        return new JsonResult(1);
    }
    @RequestMapping("/addRuzhi")
    public JsonResult<Void> addRuzhi(Ruzhi ruzhi){
        userService.insertRuzhi(ruzhi);
        return new JsonResult(1);
    }
    @RequestMapping("/listRuzhi")
    public JsonResult<Void> listRuzhi(String gonghao){
        List<Ruzhi> list = userService.listRuzhi(gonghao);
        return new JsonResult(1,list);
    }
    @RequestMapping("/updJQRuzhi")
    public JsonResult<Void> updJQRuzhi() {
        return new JsonResult(1);
    }
    @RequestMapping("/selectInfoRuzhi")
    public JsonResult<Void> selectInfoRuzhi(String id){
        Ruzhi ruzhi = userService.listByIdRuzhi(id);
        return new JsonResult(1,ruzhi);
    }
    @RequestMapping("/updateRuzhi")
    public JsonResult<Void> updateRuzhi(Ruzhi ruzhi){
        userService.updateRuzhi(ruzhi);
        return new JsonResult(1);
    }
}
